package top.zlhy7.lock;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

/**
 * @author renyong
 * @date 2022/8/13 13:55
 * @description 测试callable
 */
@Slf4j
public class ThreadDemo7 {
    @Test
    public void test1() throws Exception{
        Callable<String> callable = ()->Thread.currentThread().getName()+" 你好世界";
        FutureTask<String> futureTask = new FutureTask<>(callable);
        new Thread(futureTask,"t1").start();
        new Thread(futureTask,"t2").start();
        //isDone计算是否完成，
        while (!futureTask.isDone()){
            System.out.println("waiting...");
        }
        System.out.println(futureTask.get());
        // 计算一次后之后就都不用再次计算了
        System.out.println(futureTask.get());
        System.out.println(futureTask.get());
        System.out.println(futureTask.get());
    }
}
